package com.kanshu.ksgb.fastread.doudou.module.book.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.view.CustomGridview;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.CategoryBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedCategoryBean;

import java.util.List;

/**
 * Created by wcy on 2017\7\14 0014.
 */

public class CategoryAdapter extends BaseQuickAdapter<SelectedCategoryBean> {
    SparseArray<SubCategoryGridAdapter> mArray = new SparseArray<>();
    public CategoryAdapter(Context context) {
        super(context);
    }

    public CategoryAdapter(Context context, List<SelectedCategoryBean> data) {
        super(context, data);
    }


    @Override
    protected int attachLayoutRes() {
        return R.layout.item_category_layout;
    }

    @Override
    protected void convert(BaseViewHolder holder, SelectedCategoryBean item, int position) {
        holder.setText(R.id.first_category_label, item.name);
        CustomGridview gridview = holder.getView(R.id.grid);
        if (Utils.isEmptyList(item.list)) {
            gridview.setVisibility(View.GONE);
        } else  {
            gridview.setVisibility(View.VISIBLE);
            SubCategoryGridAdapter adapter = new SubCategoryGridAdapter(mContext, R.layout.item_sub_category_layout, item.list);
            if (mArray.get(position) == null) {
                gridview.setAdapter(adapter);
                mArray.put(position, adapter);
            } else {
                mArray.get(position).notifyDataSetChanged();
            }
        }

    }
}
